﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Fantasy_Fall_Level_Editor
{
    public partial class Layer_Window : Form
    {
        public event EventHandler ListBoxChange;
        public event EventHandler VisibleChange;
        public event EventHandler LayerAdded;

        List<Layer> layers;

        int selectedLayerIndex = -1;
        public Layer_Window()
        {
            InitializeComponent();

            this.ControlBox = false;
        }

        public List<Layer> Layers
        {
            get { return layers; }
            set { layers = value; }
        }

        public int SelectedLayerIndex
        {
            get { return selectedLayerIndex; }
            set { selectedLayerIndex = value; }
        }

        public void ChangeTextBox(string name)
        {
            Name_textBox.Text = name;

            Add_Button_Click(this, EventArgs.Empty);
        }

        private void ListBox1_Changed(object sender, EventArgs e)
        {
            if (ListBoxChange != null)
            {
                ListBoxChange(this, EventArgs.Empty);
            }
        }

        private void Visible_Checkbox_Changed(object sender, EventArgs e)
        {
            if(Visible_checkBox != null)
            {
                VisibleChange(this, EventArgs.Empty);
            }
        }

        private void Layer_Added(object sender, EventArgs e)
        {
            if(LayerAdded != null)
            {
                LayerAdded(this, EventArgs.Empty);
            }
        }

        private void Add_Button_Click(object sender, EventArgs e)
        {
            if (Name_textBox.Text != "")
            {
                Layer l = new Layer();

                l.Name = Name_textBox.Text;

                l.Visible = Visible_checkBox.Checked;

                listBox1.Items.Add(l.Name);

                layers.Add(l);

                listBox1.SelectedIndex = listBox1.Items.Count - 1;

                selectedLayerIndex = listBox1.SelectedIndex;

                if (LayerAdded != null)
                {
                    LayerAdded(this, EventArgs.Empty);
                }
            }
        }

        private void Remove_button_Click(object sender, EventArgs e)
        {
            layers.RemoveAt(listBox1.SelectedIndex);
            listBox1.Items.RemoveAt(listBox1.SelectedIndex);

            Name_textBox.Text = "";
            Visible_checkBox.Checked = false;
        }

        private void Move_up_button_Click(object sender, EventArgs e)
        {
            if(listBox1.SelectedIndex != -1 && listBox1.SelectedIndex != 0)
            {
                Layer k = layers[listBox1.SelectedIndex];
                Layer l = layers[listBox1.SelectedIndex - 1];

                layers[listBox1.SelectedIndex] = l;
                layers[listBox1.SelectedIndex - 1] = k;

                listBox1.Items[listBox1.SelectedIndex] = l.Name;
                listBox1.Items[listBox1.SelectedIndex - 1] = k.Name;

                listBox1.SelectedIndex = listBox1.SelectedIndex - 1;

                selectedLayerIndex = listBox1.SelectedIndex;
            }
        }

        private void Move_down_button_Click(object sender, EventArgs e)
        {
            if(listBox1.SelectedIndex != -1 && listBox1.SelectedIndex != listBox1.Items.Count - 1)
            {
                Layer k = layers[listBox1.SelectedIndex];
                Layer l = layers[listBox1.SelectedIndex + 1];

                layers[listBox1.SelectedIndex] = l;
                layers[listBox1.SelectedIndex + 1] = k;

                listBox1.Items[listBox1.SelectedIndex] = l.Name;
                listBox1.Items[listBox1.SelectedIndex + 1] = k.Name;

                listBox1.SelectedIndex = listBox1.SelectedIndex + 1;

                selectedLayerIndex = listBox1.SelectedIndex;
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                Layer l = layers[listBox1.SelectedIndex];
                l.Name = (string)listBox1.Items[listBox1.SelectedIndex];

                selectedLayerIndex = listBox1.SelectedIndex;
                Visible_checkBox.Checked = l.Visible;

                Name_textBox.Text = l.Name;

                if (ListBoxChange != null)
                {
                    ListBoxChange(this, EventArgs.Empty);
                }
            }
        }

        private void Visible_checkBox_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                Layer l = layers[listBox1.SelectedIndex];

                l.Name = (string)listBox1.Items[listBox1.SelectedIndex];

                l.Visible = !l.Visible;

                listBox1.Items[listBox1.SelectedIndex] = l.Name;

                layers[listBox1.SelectedIndex] = l;

                if (Visible_checkBox != null)
                {
                    VisibleChange(this, EventArgs.Empty);
                }
            }
        }

        private void Change_button_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                Layer l = new Layer();

                l.Name = Name_textBox.Text;

                listBox1.Items[listBox1.SelectedIndex] = l.Name;

                layers[listBox1.SelectedIndex] = l;
            }
        }
    }
}
